package org.dstart.draw.cutils
{
	import flash.display.CapsStyle;
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	
	import org.dstart.draw.cglobal.GColors;
	
	public class CGraphics extends Object
	{
		public var graphics:Graphics;		//	从属的绘画对象
		
		private var m_lineColor:uint;		//	填充的线条颜色（单一颜色）
		private var m_fillHightColor:uint;	//	填充颜色（高亮部份）
		private var m_fillLowColor:uint;	//	填充颜色（暗调颜色）
		private var m_lineStyle:uint;		//	线条厚度
		
		public function CGraphics(g:Graphics = null, lc:Number = -1, fhc:Number = -1, flc:Number = -1, ls:Number = 1)
		{			
			graphics = g;
			
			this.lineColor = lc;
			this.hightColor = fhc;
			this.lowColor = flc;
			this.lineStyle = ls;
		}
		
		//	设置线条颜色
		public function set lineColor(color:Number):void
		{
			m_lineColor = (color < 0) ? GColors.DEF_LINE_COLOR : uint(color);
		}
		
		//	设置填充区高亮部份颜色
		public function set hightColor(color:Number):void
		{
			m_fillHightColor = (color < 0) ? GColors.DEF_F_HIGHT_COLOR : uint(color);
		}
		
		//	设置填充区暗色调部份颜色
		public function set lowColor(color:Number):void
		{
			m_fillLowColor = (color < 0) ? GColors.DEF_F_LOW_COLOR : uint(color);
		}
		
		//	设置线条粗细
		public function set lineStyle(num:Number):void
		{
			m_lineStyle = num;
		}
		
		//	绘制一个矩形框
		public function DrawRectangle(	rect:Rectangle = null, 
										r:Number = 5, 
										model:Number = 0, 
										isFill:Boolean = true, 
										isLine:Boolean = true,
										rotation:Number = 0):void
		{
			if(this.graphics)
			{
				if(!rect)
				{
					rect = new Rectangle(0, 0, 100, 100);
				}			
				
				SetDrawModel(isFill, isLine, rect, rotation);
				
				switch(model)
				{
					//	所有方位都绘有圆角
					case CGraphicsRoundModel.ALL_ROUND :
						this.graphics.drawRoundRectComplex(rect.x, rect.y, rect.width, rect.height, r, r, r, r);
						break;
					
					//	上方圆角
					case CGraphicsRoundModel.UP_ROUND :
						this.graphics.drawRoundRectComplex(rect.x, rect.y, rect.width, rect.height, r, r, 0, 0);
						break;
	
					//	下方圆角
					case CGraphicsRoundModel.DOWN_ROUND :
						this.graphics.drawRoundRectComplex(rect.x, rect.y, rect.width, rect.height, 0, 0, r, r);
						break;
	
					//	左侧圆角
					case CGraphicsRoundModel.LEFT_ROUND :
						this.graphics.drawRoundRectComplex(rect.x, rect.y, rect.width, rect.height, r, 0, r, 0);
						break;
	
					//	右侧圆角
					case CGraphicsRoundModel.RIGHT_ROUND :
						this.graphics.drawRoundRectComplex(rect.x, rect.y, rect.width, rect.height, 0, r, 0, r);
						break;
					
					default :
						this.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
						break;
				}
							
				this.graphics.endFill();
			}	
		}		
		
		//	设置绘画及填充模式
		private function SetDrawModel(isFill:Boolean, isLine:Boolean, rect:Rectangle, r:Number):void
		{
			if(this.graphics)
			{				
				if(isLine)
				{
					this.graphics.lineStyle(this.m_lineStyle, this.m_lineColor, 1, true, LineScaleMode.NONE, CapsStyle.ROUND, JointStyle.ROUND);
				}
				if(isFill)
				{
					var colors:Array = [this.m_fillHightColor, this.m_fillLowColor, this.m_fillHightColor];
					var alphas:Array = [1, 1, 1];
					var rotations:Array = [0x00, 0x3F, 0xFF];
					var matrix:Matrix = new Matrix();
					matrix.createGradientBox(rect.width, rect.height, Math.PI * (r/180));
					
					this.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, rotations, matrix);
				}
			}
		}		
	}
}